const swaggerJsdoc = require('swagger-jsdoc');
const swaggerUi = require('swagger-ui-express');
const path = require('./swagger/base');
const common = require('./swagger/common');

const options = {
  definition: {
    openapi: '3.0.0',
    info: {
      title: 'Express API with Swagger',
      version: '1.0.0',
      description: 'This is a simple Express API application made with Swagger',
    },
    servers: [
      {
        url: 'http://localhost:8000',
        description: 'Development server',
      },
    ],
    tags: [
      {
        name: '用户信息',
        description: '用户信息相关操作',
      },
      {
        name: '公共接口',
        description: '文件上传等公共接口',
      },
      {
        name: '商品信息',
        description: '商品类型相关接口',
      },
    ],
    paths: {...path,...common},
    components: {
     securitySchemes: {
       bearerAuth: {
         type: 'http',
         scheme: 'bearer',
         bearerFormat: 'JWT',
       },
     },
   },
   security: [
     {
       bearerAuth: [],
     },
   ],
  },
  apis: ['./routes/*.js'], // 你的路由文件路径
};

const specs = swaggerJsdoc(options);

module.exports = {
 specs,
 serve: swaggerUi.serve,
 setup: swaggerUi.setup(specs, {
   explorer: true,
   // 指定你的自定义 CSS 文件的路径
  //  customCss:"./swagger/customer.css",
  //  swaggerUrl: 'http://localhost:8000/api-docs',
   swaggerOptions: {
     requestInterceptor: (req) => {
       const token = localStorage.getItem('token'); // 假设token存储在localStorage中
       if (token) {
         req.headers.Authorization = `Bearer ${token}`;
       }
       return req;
     },
   },
 }),
};
